﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace M
{
	/// <summary>
	/// 提供一个简单的Output类型
	/// 可用于引用程序调试/跟踪使用
	/// </summary>
	[Serializable]
	public class MOuput
	{
		/// <summary>
		/// 创建一个Output实例
		/// </summary>
		/// <param name="key">实例名</param>
		public MOuput(string key)
		{
			MValidator.CheckForEmptyString("Output实例名", key);
			// 向管理器注册
			this.Key = key;
			MOutputManager.Manager.RegisterOutput(this);
		}

		/// <summary>
		/// 输出
		/// </summary>
		/// <param name="output">输出字符串</param>
		public void Out(string output)
		{
			if (output == null) output = string.Empty;
			MOutputManager.Manager.WriteOutput(this.Key, output);
		}
		/// <summary>
		/// 输出
		/// </summary>
		/// <param name="format">输出字符串模版</param>
		/// <param name="args">模版参数</param>
		public void OutFormat(string format, params object[] args)
		{
			this.Out(string.Format(format, args));
		}

		/// <summary>
		/// 实例名
		/// </summary>
		public string Key { get; private set; }

		/// <summary>
		/// 设置默认的输出动作
		/// </summary>
		/// <param name="outputAction"></param>
		public static void SetDefaultOutputAction(Action<string, string> outputAction)
		{
			MOutputManager.Manager.SetOutputAction(outputAction);
		}
	}
}
